<template>
  <div class="auth-container">
    <el-card class="auth-card" shadow="hover">
      <div class="auth-header">
        <h2>创建账户</h2>
        <p>加入中医智能健康助手平台</p>
      </div>
      <el-form ref="formRef" :model="form" :rules="rules" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" autocomplete="username" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="可选，便于找回密码" autocomplete="email" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="可选，用于健康提醒" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            show-password
          />
        </el-form-item>
        <el-button type="primary" class="auth-submit" :loading="loading" @click="handleSubmit">
          立即注册
        </el-button>
      </el-form>
      <div class="auth-footer">
        已有账号？
        <router-link to="/login">马上登录</router-link>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage } from 'element-plus';
import { registerApi } from '@/api/auth';
import type { RegisterRequest } from '@/types/auth';

const router = useRouter();

const formRef = ref<FormInstance>();
const loading = ref(false);

const form = reactive<RegisterRequest>({
  username: '',
  password: '',
  confirmPassword: '',
  email: '',
  phone: '',
});

const rules: FormRules<RegisterRequest> = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 4, max: 50, message: '用户名长度为4-50字符', trigger: 'blur' },
  ],
  email: [{ type: 'email', message: '邮箱格式不正确', trigger: 'blur' }],
  phone: [
    {
      validator: (_rule, value, callback) => {
        if (!value) {
          callback();
          return;
        }
        const regex = /^1[3-9]\d{9}$/;
        if (!regex.test(value)) {
          callback(new Error('手机号格式不正确'));
          return;
        }
        callback();
      },
      trigger: 'blur',
    },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 64, message: '密码长度为6-64字符', trigger: 'blur' },
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (_rule, value, callback) => {
        if (value !== form.password) {
          callback(new Error('两次输入的密码不一致'));
          return;
        }
        callback();
      },
      trigger: 'blur',
    },
  ],
};

const handleSubmit = () => {
  formRef.value?.validate(async (valid) => {
    if (!valid) return;
    loading.value = true;
    try {
      await registerApi(form);
      ElMessage.success('注册成功，请登录');
      router.replace('/login');
    } finally {
      loading.value = false;
    }
  });
};
</script>

<style scoped lang="scss">
.auth-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #d7e3fc 0%, #f5f7fa 100%);
}

.auth-card {
  width: 460px;
  padding: 1.5rem 1.75rem 2rem;
  border-radius: 16px;
}

.auth-header {
  text-align: center;
  margin-bottom: 1.5rem;

  h2 {
    margin: 0;
    font-size: 1.6rem;
    font-weight: 600;
    color: #1f2937;
  }

  p {
    margin: 0.5rem 0 0;
    color: #6b7280;
    font-size: 0.95rem;
  }
}

.auth-submit {
  width: 100%;
  margin-top: 0.5rem;
  height: 44px;
  font-size: 1rem;
  letter-spacing: 0.1em;
}

.auth-footer {
  margin-top: 1rem;
  text-align: center;
  color: #6b7280;

  a {
    margin-left: 0.25rem;
    color: #2563eb;
    font-weight: 500;
  }
}
</style>

